package top.codedance.iotp.client.plugins.sync.client;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.client.ClientApplication;
import top.codedance.iotp.common.executer.SyncAbstractExecuter;
import top.codedance.iotp.common.entity.PluginResultEntity;

import java.util.Map;
import java.util.Set;

public class ClientPluginsParamsGetExecuterImpl extends SyncAbstractExecuter {
    @Override
    public PluginResultEntity exec(byte[] data, String taskId) {
        PluginResultEntity result = new PluginResultEntity();
        Map<String, Map<String, String>> params = ClientApplication.readConfig();
        JSONObject jo = new JSONObject();
        Set<Map.Entry<String, Map<String, String>>> entries = params.entrySet();
        for(Map.Entry<String, Map<String, String>> item : entries){
            Map<String, String> value = item.getValue();
            Set<Map.Entry<String, String>> subEntries = value.entrySet();
            JSONObject subJo = new JSONObject();
            for(Map.Entry<String, String> subItem : subEntries){
                subJo.put(subItem.getKey(), subItem.getValue());
            }
            jo.put(item.getKey(), subJo);
        }
        try {
            result.setOk(true);
            result.setContext(jo);
        }catch (Exception ex){
            result.setOk(false);
            result.setMsg(ex.getMessage());
        }
        return result;
    }
}
